/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2023-12-07 09:26:03
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2023-12-20 10:27:06
 * @FilePath: /yt-devops/lib/pages/tasks/report_task_detail/report_task_detail_controller.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:native_dart_package/loading/loading_util.dart';
import 'package:yt_devops/configs.dart';
import 'package:yt_devops/model/user_model.dart';
import 'package:yt_devops/pages/tasks/create_repair_task/repair_task_operate_enum.dart';
import 'package:yt_devops/pages/tasks/report_task_detail/report_task_detail_model.dart';
import 'package:yt_devops/pages/tasks/task_list/task_list_controller.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/services/task/task_services.dart';
import 'package:yt_devops/utils/enums.dart';

class ReportTaskDetailController extends GetxController {
  TaskListController taskListController = Get.find();

  ReportTaskDetailModel model = ReportTaskDetailModel();

  final TaskServices services = Get.find();

  UserModel? selectUser;

  int? taskId;

  @override
  void onInit() {
    super.onInit();
    taskId = Get.arguments['id'];
    loadDetail(taskId!);
  }

  @override
  void onReady() {}

  @override
  void onClose() {}

  loadDetail(int id) => Configs.httpClient.requestWithLoading(() async {
        var params = {'faultId': id};
        showLoading(toast: ResKey.loading.tr);
        var data = await services.getReportTaskDetail(params);
        dismissLoading();
        model = ReportTaskDetailModel.fromJson(data);
        update();
      });

  accept(id, TaskType type) => Configs.httpClient.requestWithLoading(() async {
        showLoading(toast: ResKey.loading.tr);
        await services.acceptReportTask({'id': id});
        dismissLoading();
        BrnToast.show(
          ResKey.taskProcessSuccessTip.tr,
          Get.context!,
          preIcon: Image.asset(
            ImageAssets.iconSuccess,
            width: 24,
            height: 24,
          ),
          duration: BrnDuration.short,
          onDismiss: () {
            loadDetail(id);
            taskListController.refreshReport();
          },
        );
      }, showLoading: false);

  toCreateRepairOrder() {
    Get.toNamed(RouterPath.createRepairTask, arguments: {
      'type': RepairTaskTypeOperateEnum.createFromRepair,
      'id': model.id,
      'siteId': model.siteId,
      'siteName': model.siteName,
      'name': model.name,
      'content': model.content,
      'userName': model.userName,
      'userId': model.userId
    })?.then((value) {
      if (value == true) {
        loadDetail(taskId!);
        taskListController.refreshReport();
      }
    });
  }

  toFinish() {
    Get.toNamed(RouterPath.finishRepairReason, arguments: {
      'id': taskId,
    })?.then((value) {
      if (value == true) {
        loadDetail(taskId!);
        taskListController.refreshReport();
      }
    });
  }

  checkMaintainOrder() {
    if (model.maintenanceId != null) {
      Get.toNamed(RouterPath.repairTaskDetail,
          arguments: {'id': model.maintenanceId});
    }
  }
}
